package com.itheima.userconsumer.controller;

import com.itheima.userconsumer.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class UserController {
    @Autowired
    UserService userService;

    //跳转登录界面
    @GetMapping(value = "/tologin")
    public String tologin() {
        return "login";
    }

    // 跳转注册界面
    @GetMapping(value = "/toregister")
    public String toregister() {

        return "register";
    }

    // 用户登录
    @GetMapping(value = "/login")
    public String login(String uname, String upassword, HttpServletRequest request) {
        System.out.println("user-consumer-UserController:" + uname);
        System.out.println("user-consumer-UserController:" + upassword);
        System.out.println("user-consumer-UserController:" + userService.login(uname));
        if (userService.login(uname) != null) {
            if (userService.login(uname).getUpassword().equals(upassword)) {
                System.out.println(userService.login(uname));

                HttpSession session = request.getSession(true);
                session.setAttribute("user", userService.login(uname));  //将登陆者信息存入session
                System.out.println("user-consumer UserController:登录成功！！");
                return "redirect:http://localhost:8896/goods/getAll?uaccount=" +
                        userService.login(uname).getUaccount() + "&upassword=" + userService.login(uname).getUpassword();
            } else {
                System.out.print("user-consumer UserController:密码错误，重新登陆！");
                return "login";
            }
        }
        //request.setAttribute("mag","用户不存在，请注册");
        System.out.print("user-consumer UserController:用户不存在，请注册");
        return "register";
    }

    //用户注册
    @GetMapping(value = "/register")
    public String register(HttpServletRequest request, String upassword, String uname, String usex) {
        int register = userService.register(upassword, uname, usex);
        if (register > 0) {
            System.out.print("register:注册成功");
            return "login";
        }
        System.out.print("注册失败");
        return "register";
    }
}